import java.applet.*;
import java.awt.*;
import java.util.*;
import java.lang.*;
import java.text.*;
import java.awt.event.*; 
import java.io.*;
import net.sf.saxon.*;
import javax.xml.transform.*;
 
public class XSLT2 extends Applet implements ActionListener {
	Button openButton;
	Transform xsltproc;
	/** base directory of application, set to parent of application-directory */
	String baseDir;
	public XSLT2() {
		Panel p = new Panel();
		openButton = new Button("Open");
		openButton.addActionListener(this);
		p.add(openButton);
		this.add(p);

		xsltproc = new Transform();
		xsltproc.now();
	}
  public void start() {
		java.net.URL url = getCodeBase();
		baseDir = (new File(this.getCodeBase().getPath())).getParentFile().getPath().replaceAll("\\\\","\\/")+'/';
		this.add(new Label('-'+baseDir+'-'))	;
		transformEBSchema("schemas/PurchaseOrder.xsd");
		try { 
    Object[] args = { this };
    netscape.javascript.JSObject.getWindow(this).call("appletLoaded", args );
		} catch(Exception e) {} // to make testing inside appletviewer possible.
  }
  public String transformEBSchema(String ebschema) {
    final StringWriter res = new StringWriter();
    final String febschema = ebschema;
  	java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<String>() {
      public String run() {
		  	try {
		  		StandardURIResolver urir = new StandardURIResolver(new net.sf.saxon.Configuration());
		  		TransformerFactoryImpl tfi = new TransformerFactoryImpl();
		  		Source fxsl = (Source) urir.resolve("stylesheets/Message.xsl", baseDir);
			  	Controller xsl = (Controller) tfi.newTransformer(fxsl);
			  	Source xsd = (Source) xsltproc.loadDocuments(
			  	 baseDir + febschema,
			  	 false, null, false);
			  	javax.xml.transform.stream.StreamResult resr = new javax.xml.transform.stream.StreamResult(res);
			  	xsl.transform(xsd, resr);
			  	
		//  	} catch(IOException e) {
		//  		throw new Error(e);
		//  	} catch(ClassNotFoundException e) {
		//  		throw new Error(e);
		  	} catch (TransformerException e) {
		  		System.out.println(e.getCause() );
		  	  throw new Error(e);	
		  	}
		//  	xsltproc.processFile(xsd, 
      	return res.toString();
      }
  	});
  	return res.toString();
  }
  
  
  	 public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Open")) {
			System.out.println("OPEN CLICKED");
			
			int arrlen = 10000;
			byte[] infile = new byte[arrlen];
			Frame parent = new Frame();
			FileDialog fd = new FileDialog(parent, "Please choose a file:",
			    FileDialog.LOAD);
			fd.setVisible(true);
			String selectedItem = fd.getFile();
			if (selectedItem == null) {
				// no file selected
			} else {
				File ffile = new File( fd.getDirectory() + File.separator +
				                     fd.getFile());
				// read the file
				System.out.println("reading file " + fd.getDirectory() +
				                        File.separator + fd.getFile() );
				try {
					FileInputStream fis = new FileInputStream(ffile); 
					BufferedInputStream bis = new BufferedInputStream(fis);
					DataInputStream dis = new DataInputStream(bis);
					try {
						int filelength = dis.read(infile);
						String filestring = new String(infile, 0,
						                           filelength);
						System.out.println("FILE CONTENT=" + filestring);
					} catch(IOException iox) {
						System.out.println("File read error...");
						iox.printStackTrace();
					}
				} catch (FileNotFoundException fnf) {
					System.out.println("File not found...");
					fnf.printStackTrace();
				}
			}		
			
		}

	}
  
}